package com.myraceengineer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	
	ListView listView;
	private static int VALID = 6;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_list_view);
		
		listView = (ListView) findViewById(R.id.list);
		
		// Defined Array values to show in ListView
        String[] circuits = new String[] { "Catalunya", 
                                         "Le Mans",
                                         "Laguna Seca",
                                         "Road Atlanta", 
                                         "Silverstone", 
                                         "Magny-Cours", 
                                         "Nurburgring" 
                                        };
        
        ArrayAdapter<String> circuitsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, circuits);
        
        // Assign adapter to ListView
        listView.setAdapter(circuitsAdapter); 
        
     // ListView Item Click Listener
        listView.setOnItemClickListener(new OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {
                
            	  Intent intent = new Intent(MainActivity.this, SessionsActivity.class);
            	  	if(position == VALID) {
            	  		startActivity(intent);
            	  	}
            	  	else {
            	  		AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                        builder1.setMessage("No sessions found.");
                        builder1.setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                            @Override
							public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

                        AlertDialog alert11 = builder1.create();
                        alert11.show();
            	  	}
             
              	}

         }); 

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
